{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5301f8d5-25e8-434e-b367-7bceebf326a7",
   "metadata": {},
   "source": [
    "<center><img src=\"./image/CLBLOGO.jpg\" alt=\"创乐博\" style=\"width: 300px;\"/></center>\n",
    "\n",
    "# 9-2.无源蜂鸣器实验\n",
    "\n",
    "@－－－－湖南创乐博智能科技有限公司－－－－<br>\n",
    "@  文件名：9-2.无源蜂鸣器实验.py <br>\n",
    "@  版本：V2.0 <br>\n",
    "@  author: zhulin<br>\n",
    "@  说明：无源蜂鸣器实验<br>\n",
    "无源蜂鸣器实验，通过GPIOZero库的TonalBuzzer专用音频合成库直接驱动无源蜂鸣器发出具有频率信息的声音！！！<br>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5f58331-df2f-409c-b08b-1401a5100a80",
   "metadata": {},
   "source": [
    "## 1.导入必要的库文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "24bb5df6-9701-49a1-8df4-dcbd1a87c5fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "from gpiozero import TonalBuzzer\n",
    "from gpiozero.tones import Tone\n",
    "from time import sleep\n",
    "from signal import pause"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf81ed90-ea51-4447-b522-85494d089b54",
   "metadata": {},
   "source": [
    "## 2.定义无源蜂鸣器管脚"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20561d89-e334-41e0-9358-07f9b385ffdd",
   "metadata": {},
   "outputs": [],
   "source": [
    "makerobo_Buzzer = 17    # 有源蜂鸣器管脚定义"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "201cd995-24c2-4b38-a042-2d688d0e8cfb",
   "metadata": {},
   "source": [
    "## 3.音谱定义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c5e25349-e115-46d7-a87a-b5909d719b88",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 音谱定义\n",
    "Tone_CL = [220, 220, 220, 220, 220, 220, 220, 248]\t\t# 低C音符的频率\n",
    "Tone_CM = [220, 262, 294, 330, 350, 393, 441, 495]\t\t# 中C音的频率\n",
    "Tone_CH = [220, 525, 589, 661, 700, 786, 800, 880]\t\t# 高C音符的频率\n",
    "\n",
    "# 第一首歌音谱\n",
    "makerobo_song_1 = [\tTone_CM[3], Tone_CM[5], Tone_CM[6], Tone_CM[3], Tone_CM[2], Tone_CM[3], Tone_CM[5], Tone_CM[6], \n",
    "\t\t\t        Tone_CH[1], Tone_CM[6], Tone_CM[5], Tone_CM[1], Tone_CM[3], Tone_CM[2], Tone_CM[2], Tone_CM[3], \n",
    "\t\t\t        Tone_CM[5], Tone_CM[2], Tone_CM[3], Tone_CM[3], Tone_CL[6], Tone_CL[6], Tone_CL[6], Tone_CM[1],\n",
    "\t\t\t        Tone_CM[2], Tone_CM[3], Tone_CM[2], Tone_CL[7], Tone_CL[6], Tone_CM[1], Tone_CL[5]\t]\n",
    "# 第1首歌的节拍，1表示1/8拍\n",
    "makerobo_beat_1 = [\t1, 1, 3, 1, 1, 3, 1, 1, \t\t\t\n",
    "\t\t\t        1, 1, 1, 1, 1, 1, 3, 1, \n",
    "\t\t\t        1, 3, 1, 1, 1, 1, 1, 1, \n",
    "\t\t\t        1, 2, 1, 1, 1, 1, 1, 1, \n",
    "\t\t\t        1, 1, 3\t]\n",
    "# 第二首歌音谱\n",
    "makerobo_song_2 = [\tTone_CM[1], Tone_CM[1], Tone_CM[1], Tone_CL[5], Tone_CM[3], Tone_CM[3], Tone_CM[3], Tone_CM[1],\n",
    "\t\t\t        Tone_CM[1], Tone_CM[3], Tone_CM[5], Tone_CM[5], Tone_CM[4], Tone_CM[3], Tone_CM[2], Tone_CM[2], \n",
    "\t\t\t        Tone_CM[3], Tone_CM[4], Tone_CM[4], Tone_CM[3], Tone_CM[2], Tone_CM[3], Tone_CM[1], Tone_CM[1], \n",
    "\t\t\t        Tone_CM[3], Tone_CM[2], Tone_CL[5], Tone_CL[7], Tone_CM[2], Tone_CM[1]\t]\n",
    "\n",
    "# 第2首歌的节拍，1表示1/8拍\n",
    "makerobo_beat_2 = [\t1, 1, 2, 2, 1, 1, 2, 2, \t\t\t\n",
    "\t\t\t        1, 1, 2, 2, 1, 1, 3, 1, \n",
    "\t\t\t        1, 2, 2, 1, 1, 2, 2, 1, \n",
    "\t\t\t        1, 2, 2, 1, 1, 3 ]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "948171fb-35a4-4e99-a671-172c4c27a411",
   "metadata": {},
   "source": [
    "## 3.初始化工作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a0c58259-8f1b-48ee-beed-cd91d6cef020",
   "metadata": {},
   "outputs": [],
   "source": [
    "# GPIO设置函数\n",
    "def makerobo_setup():\n",
    "    global bz\n",
    "    bz = TonalBuzzer(makerobo_Buzzer)  # 设置管脚\n",
    "    bz.stop()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "04b01200-b8f4-4906-9fef-ee5921375822",
   "metadata": {},
   "source": [
    "## 4.功能函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff0e6589-219b-4173-8828-2a7e9b97f733",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 循环函数\n",
    "def makerobo_loop():\n",
    "\twhile True:\n",
    "\t\t#    播放第一首歌音乐...\n",
    "\t\tfor i in range(1, len(makerobo_song_1)):     # 播放第一首歌\n",
    "\t\t\tbz.play(Tone(makerobo_song_1[i]))              # 设置歌曲音符的频率\n",
    "\t\t\tsleep(makerobo_beat_1[i] * 0.5)\t         # 延迟一个节拍* 0.5秒的音符\n",
    "\t\tsleep(1)\t\t\t\t\t\t             # 等待下一首歌。\n",
    "\n",
    "\t\t#    播放第二首歌音乐...\n",
    "\t\tfor i in range(1, len(makerobo_song_2)):     # 播放第二首歌\n",
    "\t\t\tbz.play(Tone(makerobo_song_2[i]))              # 设置歌曲音符的频率\n",
    "\t\t\tsleep(makerobo_beat_2[i] * 0.5)          # 延迟一个节拍* 0.5秒的音符\n",
    "\n",
    "# 释放资源函数\n",
    "def makerobo_destory():\n",
    "\tbz.stop()\t\t\t    # 停止蜂鸣器"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15ed888c-608f-4db1-8e2c-4883b91123f2",
   "metadata": {},
   "source": [
    "## 5.主程序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f021e634-cfe9-4752-83e9-9a773069d42e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 程序入口\n",
    "if __name__ == '__main__':\t\t\n",
    "\tmakerobo_setup()\n",
    "\ttry:\n",
    "\t\tmakerobo_loop()\n",
    "\texcept KeyboardInterrupt:  \t# 当按下Ctrl+C时，将执行destroy()子程序。\n",
    "\t\tmakerobo_destory()      # 释放资源"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20858ae4-b03a-4008-8832-187a6fc11193",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
